from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import random

colored = [
    ('#2b5b56', '#59847f'), ('#8bc34a', '#a9d379'),
    ('#50634f', '#809e7e'), ('#cc7b66', '#e3b6aa'),
    ('#ff4d4d', '#ff9999'), ('#999966', '#b8b894'),
    ('#00ccff', '#4ddbff'), ('#ff99cc', '#ffb3d9'),
    ('#cc99ff', '#e5ccff'), ('#669999', '#94b8b8')
]
class TagsEditor(QtWidgets.QWidget):
    def __init__(self, tags=[], lenLimit=255):
        super(TagsEditor, self).__init__()
        self.lenth = lenLimit
        self.tags = tags
        self.init_ui()

    def init_ui(self):
        self.layout = QtWidgets.QFormLayout()
        self.novelTagsLabel = QtWidgets.QLabel('Tags')
        self.novelTagsEdit = QtWidgets.QLineEdit()

        self.novelTaggedLabel = QtWidgets.QLabel('Tagged')
        self.novelTaggedFrame = QtWidgets.QFrame()
        self.novelTaggedLayout = QtWidgets.QHBoxLayout()
        self.novelTaggedFrame.setLayout(self.novelTaggedLayout)

        self.layout.addRow(self.novelTagsLabel, self.novelTagsEdit)
        self.layout.addRow(self.novelTaggedLabel, self.novelTaggedFrame)
        self.setLayout(self.layout)

        if len(self.tags):
            for tag in self.tags:
                tag = Tag(tag)
                tag.closeSignal[str].connect(self.update_tags)
                self.novelTaggedLayout.addWidget(tag, 0, QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)

        self.novelTagsEdit.returnPressed.connect(self.on_tags_edit)

    def on_tags_edit(self):
        tagsLen = len(str(self.tags))
        if tagsLen > self.lenth:
            raise Exception('Tags length is too long, over 255')
        tag = Tag(self.novelTagsEdit.text())
        tag.closeSignal[str].connect(self.update_tags)
        self.novelTaggedLayout.addWidget(tag, 0, QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
        self.tags.append(self.novelTagsEdit.text())
        self.novelTagsEdit.clear()

    def update_tags(self, tag):
        self.tags.remove(tag)
        print(self.tags)

    def get_tags(self):
        return self.tags


class Tag(QtWidgets.QWidget):
    closeSignal = pyqtSignal(str)

    def __init__(self, tag):
        super(Tag, self).__init__()
        self.choose_color()
        self.color = '#ffffff'
        self.fontSize = '10px'
        self.fontFamily = 'Microsoft YaHei'
        self.labelStyleSheet = "QLabel { font-family: '" + self.fontFamily + "';" + \
                               "font-size: " + self.fontSize + "; color: " + self.color + ";" + \
                               "font-style: bold; background-color:" + self.bgColor + ";" + \
                               "text-align: center;}"
        self.hoverStyleSheet = "QLabel { font-family: '" + self.fontFamily + "';" + \
                               "font-size: " + self.fontSize + "; color: " + self.color + ";" + \
                               "font-style: bold; background-color:" + self.hoverbgColor + ";" + \
                               "text-align: center;}"
        self.tag = tag
        self.init_ui()

    def choose_color(self):
        self.bgColor, self.hoverbgColor = random.choice(colored)

    def init_ui(self):
        self.layout = QtWidgets.QHBoxLayout()
        self.tagLabel = QtWidgets.QLabel(self.tag)
        # self.tagLabel.setFixedSize(20, 20)
        self.deleteLabel = QtWidgets.QLabel(" X ")
        self.tagLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.deleteLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.tagLabel.setFixedSize(20 + 14 * len(self.tag), 20)
        self.deleteLabel.setFixedSize(30, 20)

        self.tagLabel.setFrameShape(QtWidgets.QFrame.Box)
        self.deleteLabel.setFrameShape(QtWidgets.QFrame.Box)
        self.deleteLabel.setStyleSheet(self.labelStyleSheet)
        self.tagLabel.setStyleSheet(self.labelStyleSheet)

        self.layout.addWidget(self.tagLabel)
        self.layout.addWidget(self.deleteLabel)
        self.setLayout(self.layout)
        self.setFixedSize(50 + 14 * len(self.tag), 30)

        self.deleteLabel.mousePressEvent = self.on_delete

    def on_delete(self, event):
        self.closeSignal.emit(self.tag)
        self.deleteLater()

    def enterEvent(self, event):
        self.deleteLabel.setStyleSheet(self.hoverStyleSheet)
        self.tagLabel.setStyleSheet(self.hoverStyleSheet)

    def leaveEvent(self, event):
        self.deleteLabel.setStyleSheet(self.labelStyleSheet)
        self.tagLabel.setStyleSheet(self.labelStyleSheet)